package com.life.ai.server;


import cn.hutool.core.lang.Console;
import com.life.ai.server.common.utils.NetWorkInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Indexed;

import java.util.List;

@Slf4j
@Indexed
@SpringBootApplication
public class LifeAiApplication {
    public static void main(String[] args) {
        SpringApplication.run(LifeAiApplication.class, args);
    }

    /**
     * 项目启动输出访问地址
     */
    @Bean
    @ConditionalOnMissingBean(WebServerApplicationContext.class)
    public ApplicationRunner applicationRunner(WebServerApplicationContext context) {
        return (ApplicationArguments args) -> {
            try {
                List<String> ipAddressList = NetWorkInfoUtil.getIpv4NetworkInterfaces();
                int port = context.getWebServer().getPort();
                ipAddressList.forEach(addr -> Console.log("→  Network: http://{}:{}", addr, port));
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        };
    }
}
